<?php
sfContext::getInstance()->getConfiguration()->loadHelpers('Variable');

class customClientForm extends sfForm {

    private $_questionaire = false;    private $_asterisk     = ' *';

    public function __construct(Questionaire $questionaire, $defaults = array(), $options = array(), $CSRFSecret = null) {
        $this->_questionaire = $questionaire;

        parent::__construct($defaults, $options, $CSRFSecret);
    }

    public function configure() {
        $this->widgetSchema->setNameFormat('client_form[%s]');

        $userForm = $this->getUserInfoForm();
        $questionsForm = $this->getQuestionsForm();
        
        $this->embedForm('user_info', $userForm);
        $this->embedForm('questions', $questionsForm);


        parent::configure();
    }

    public function getErrors() {
        $result = array();

        foreach ($this->getErrorSchema() as $index => $val) {
            $result[$index] = $val->__toString();
        }
        return $result;
    }

    private function getUserInfoForm() {
        $form = new sfForm();
        
        $form->widgetSchema['name'] = new sfWidgetFormInput(array('label' => __('Name') . $this->_asterisk), array('style' => 'width: 185px;'));
        $form->widgetSchema['sex'] = new sfWidgetFormSelectRadio(array('label' => __('sex') . $this->_asterisk,'choices' => array('male' => __('male'), 'female' => __('female'))));
        // Getting languages
        $langs = array();
        
        foreach(explode("\n", variable('languages')) as $lang)
          $langs[trim($lang, "\r\n")] = trim($lang, "\r\n");

        $form->widgetSchema['native_language'] = new sfWidgetFormSelect(array('label' => __('language') . $this->_asterisk,'choices' => $langs));
        $form->widgetSchema['address'] = new sfWidgetFormTextarea(array('label' => __('address') . $this->_asterisk), array('rows' => 2, 'cols' => 28, 'style' => 'margin-left: 5px; margin-top: 3px; font-size: 11px;'));
        $form->widgetSchema['email'] = new sfWidgetFormInput(array('label' => __('email')), array('style' => 'width: 185px;'));
        $form->widgetSchema['phone'] = new sfWidgetFormInput(array('label' => __('phone') . $this->_asterisk), array('style' => 'width: 185px;'));
        
        // Getting languages
        $__education = array();
        
        foreach(explode("\n", variable('edu_var')) as $edu)
          $__education[trim($edu, "\r\n")] = trim($edu, "\r\n");
        
        $form->widgetSchema['education'] = new sfWidgetFormSelect(array('label' => __('edu_tr') . $this->_asterisk, 'choices' => $__education));
        // Getting languages
        $__age = array();
        
        foreach(explode("\n", variable('age_var')) as $age)
          $__age[trim($age, "\r\n")] = trim($age, "\r\n");
        
        $form->widgetSchema['age'] = new sfWidgetFormSelect(array('label' => __('age_tr') . $this->_asterisk, 'choices' => $__age));

        $form->validatorSchema['name'] = new sfValidatorString(array('required' => true, 'max_length' => '255'));
        $form->validatorSchema['sex'] = new sfValidatorChoice(array('required' => true, 'choices' => array('male' => 'male', 'female' => 'female')));
        $form->validatorSchema['native_language'] = new sfValidatorChoice(array('required' => true, 'choices' => $langs));
        $form->validatorSchema['address'] = new sfValidatorString();
        $form->validatorSchema['email'] = new sfValidatorEmail(array('required' => false));
        $form->validatorSchema['phone'] = new sfValidatorString();
        $form->validatorSchema['education'] = new sfValidatorChoice(array('required' => false, 'choices' => $__education));
        $form->validatorSchema['age'] = new sfValidatorChoice(array('required' => true, 'choices' => $__age));
        
        return $form;
    }
    
    private function getQuestionsForm() {
        $form = new sfForm();
                $q =$this->_questionaire->getQuestions();        
        foreach ($this->_questionaire->getQuestions() as $j => $question) {
            $answerType = $question->getAnswerType();
            $answers = $this->prepAnswers($question->getAnswers());

            if ($answerType != '') {              $req = ($question->getRequired()) ? $this->_asterisk : '';
                if ($answerType == 'number')                {
                    $form->widgetSchema[$question->getName()]    = new sfWidgetFormInput(array('label'    => ($j + 1) . '. ' . $question->getName() . $req));
                    $form->validatorSchema[$question->getName()] = new sfValidatorNumber(array('required' => $question->getRequired()));
                } else if ($answerType == 'text') {
                    $form->widgetSchema[$question->getName()] = new sfWidgetFormInput(array('label' => ($j + 1) . '. ' . $question->getName() . $req));
                    $form->validatorSchema[$question->getName()] = new sfValidatorString(array('required' => $question->getRequired()));
                } else if ($answerType == 'select') {                  $answers = array_merge(array("" => '--'), $answers);
                    $form->widgetSchema[$question->getName()] = new sfWidgetFormSelect(array('choices' => $answers, 'label' => ($j + 1) . '. ' . $question->getName() . $req));
                    $form->validatorSchema[$question->getName()] = new sfValidatorString(array('required' => $question->getRequired()));
                }else if ($answerType == 'radio') {                    $form->widgetSchema[$question->getName()] = new sfWidgetFormSelectRadio(array('choices' => $answers, 'label' => ($j + 1) . '. ' . $question->getName() . $req));                    $form->validatorSchema[$question->getName()] = new sfValidatorString(array('required' => $question->getRequired()));                }                 else if ($answerType == 'multiselect') {
                    $form->widgetSchema[$question->getName()] = new sfWidgetFormSelectCheckbox(array('choices' => $answers, 'label' => ($j + 1) . '. ' . $question->getName() . $req));
                    $form->validatorSchema[$question->getName()] = new sfValidatorString(array('required' => $question->getRequired()));
                } else if ($answerType == 'multitext') {
                    $sub = new sfForm();

                    foreach ($answers as $answer) {
                        $sub->widgetSchema[$answer] = new sfWidgetFormInput(array(), array('value' => ''));
                        $sub->validatorSchema[$answer] = new sfValidatorPass();
                    }
                    $form->embedForm($question->getName(), $sub);
                } else if ($answerType == 'multinumber') {
                    $sub = new sfForm();                                        

                    foreach ($answers as $answer) {
                        $sub->widgetSchema[$answer] = new sfWidgetFormInput(array(), array('value' => '', 'label' => $question->getName() . $req));
                        $sub->validatorSchema[$answer] = new sfValidatorPass();
                    }
                    $form->embedForm(($j + 1) . '. ' . $question->getName(), $sub);
                }
            }
        }
        
        return $form;
    }

    private function prepAnswers($answers)
    {      $result = array();
     
        foreach ($answers as $answer) {
            $result[$answer['label']] = $answer['label'];
        }

        return $result;
    }

}